package com.burgeon.backend.EnterpriseWeChat.rest.VipInfo;
/**
 * 添加采购退货单的头和明细（上海）
 */

import com.burgeon.backend.EnterpriseWeChat.entity.Referral;
import com.burgeon.backend.Rest.base.RESTBase;
import org.apache.http.util.TextUtils;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;

public class createReferral extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public createReferral(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public String getData(String cardNo, Referral referral) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(cardNo,referral), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");


        if (!"0".equals(code)) {
            throw new Exception(message);
        }
        String objectid = new JSONArray(str).getJSONObject(0).getString("objectid");
        return objectid;

    }

    private MultiValueMap Data(String cardNo, Referral referral) {
        HashMap params = new HashMap();
        params.put("'C_VIP_ID__CARDNO'",cardNo);

        if (!TextUtils.isEmpty(referral.getName())) {
            params.put("'NAME'",referral.getName());
        }
        if (!TextUtils.isEmpty(referral.getPhone())) {
            params.put("'PHONE'",referral.getPhone());
        }
        if (!TextUtils.isEmpty(referral.getName())) {
            params.put("'SP_PREFERENCES'","'"+referral.getShoppingPreferences()+"'");
        }

        params.put("table", "INTRODUCING_CUSTOMERS");//表名
        System.err.println(params);
        return DATA("ObjectCreate", params, account, password);
    }

    public static void main(String[] args) {
        createReferral g = new createReferral("39.100.32.10:55321", "cj@ecco.com", "123456");
        try {
//            RETAIL retail = new RETAIL();
//            retail.setC_STORE_ID("唐山爱琴海购物公园专卖店");
//            retail.setBILLDATE("20200616");
//            retail.setRETAILBILLTYPE("正常零售");
//            retail.setRECEIVEDESP("null");
//            g.getData(retail);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
    }
}
